package priv.mill.demo.fillter;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

@Component
public class MyFillter extends ZuulFilter{


	private static Logger 
	log = LoggerFactory.getLogger(MyFillter.class);

	@Override
	public Object run() {
		RequestContext currentContext = RequestContext.getCurrentContext();
		HttpServletRequest request = currentContext.getRequest();
		log.debug(String.format("%s>>>%s", request.getMethod(),request.getRequestURI()));
		String parameter = request.getParameter("token");
		if(parameter==null||parameter.isEmpty()) {
			currentContext.setSendZuulResponse(false);
			currentContext.setResponseStatusCode(400);
			try {
				currentContext.getResponse().getWriter().println("token is empty");
			} catch (IOException e) {
			}
		}
		log.debug("ok");
		return null;
	}
	/**
	 * 是否要过滤
	 */
	@Override
	public boolean shouldFilter() {
		return true;
	}
	/**
	 * 过滤顺序
	 */
	@Override
	public int filterOrder() {
		return 0;
	}

	/**
	 * 	pre：路由之前
		routing：路由之时
		post： 路由之后
		error：发送错误调用
	 */
	@Override
	public String filterType() {
		return "pre";
	}

}
